package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {
    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    /*查询通用设置*/
    public SettingsVo selectAllSettings() {
        //创建vo对象
        SettingsVo settingsVo = new SettingsVo();

        //获取用户id,并赋值给vo
        settingsVo.setId(UserHolder.getUserId());
        //获取用户手机号码
        settingsVo.setPhone(UserHolder.getMobile());
        //获取陌生人问题
        Question question = questionApi.findQuestionById(UserHolder.getUserId());
        settingsVo.setStrangerQuestion(question == null ? "你喜欢java吗？" : question.getTxt());
        //获取用户的app通知开关的：开闭数据
        Settings settings = settingsApi.findSettingsByUserId(UserHolder.getUserId());
        if (settings != null) {
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }
        return settingsVo;
    }


    //通知设置
    public void saveSettings(Map<String,Object> map) {
        //从页面数据map中，获取设置的属性值
        boolean likeNotification = (boolean) map.get("likeNotification");
        boolean pinglunNotification = (boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (boolean) map.get("gonggaoNotification");
        //获取当前用户的id
        Long userId = UserHolder.getUserId();
        //根据用户id，查询用户的通知设置
        Settings settings = settingsApi.findSettingsByUserId(userId);
        //判断
        if (settings == null) {
            settings = new Settings();
            //设置userId
            settings.setUserId(userId);
            //设置开关属性
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            //保存settings信息
            settingsApi.save(settings);
        } else {
            //设置开关属性
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            //更新settings信息
            settingsApi.update(settings);
        }

    }

    //取消黑名单用户
    public void deleteBlackList(Long deleteId) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //调用api执行
        blackListApi.delete(userId, deleteId);
    }

    //分页查询黑名单列表
    public PageResult blackList(int page, int size) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //调用api查询用户黑名单分页对象
        IPage<UserInfo> iPage = blackListApi.findPageById(userId, page, size);
        //对象转化，将查询的Ipage对象内容封装到pageresult
        PageResult pr = new PageResult(page, size, (int) iPage.getTotal(), iPage.getRecords());
        return pr;
    }

    //设置陌生人问题
    public void saveQuestion(String content) {
        //1、获取当前用户id
        Long userId = UserHolder.getUserId();
        //2、调用api查询当前用户的陌生人问题
        Question question = questionApi.findQuestionById(userId);
        //3、判断问题是否存在
        if (question == null) {
            //3.1 如果不存在，保存
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            //3.2 如果存在，更新
            question.setTxt(content);
            questionApi.update(question);
        }
    }
}
